<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="保险单号" prop="insuranceOdd">
            <el-input v-model="queryParams.insuranceOdd" clearable placeholder="请输入保险单号"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="保险名称" prop="insuranceName">
            <el-input v-model="queryParams.insuranceName" clearable placeholder="请输入保险名称"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="保险状态" prop="insuranceStatus">
            <el-select v-model="queryParams.insuranceStatus" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option label="在保中" value="1"></el-option>
              <el-option label="已过期" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:insurance:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">新增保险
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:insurance:export']" icon="el-icon-download" plain size="mini" type="primary"
                   @click="handleExport(1,'应购买保险人信息')">导出应购买保险人
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:insurance:export']" icon="el-icon-download" plain size="mini" type="primary"
                   @click="handleExport(2,'被替换人员信息')">导出被替换人员
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="insuranceList" border>
      <el-table-column align="center" label="保险单号" prop="insuranceOdd" width="200px;"/>
      <el-table-column align="center" label="保险名称" prop="insuranceName"/>
      <el-table-column align="center" label="保险类型" prop="insuranceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_insurance_type" :value="scope.row.insuranceType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="保险额度" prop="insuranceQuato"/>
      <el-table-column align="center" label="开始时间" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="结束时间" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="保险预警">
        <template slot-scope="scope">
          <span>{{ timeDifference(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="保险状态" prop="insuranceStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.insuranceStatus == 1" type="success">在保中</el-tag>
          <el-tag v-if="scope.row.insuranceStatus == 2" type="danger">已过期</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="在保人数" prop="insuranceNumber"/>
      <el-table-column align="center" label="可替换" prop="replaceable"/>
      <el-table-column align="center" label="总保费" prop="totalPremium">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.maxMoney + scope.row.minMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="已报案" prop="reported"/>
      <el-table-column align="center" label="已出险" prop="already"/>
      <el-table-column align="center" label="已结案" prop="caseclosed"/>
      <el-table-column align="center" label="出险率" prop="accidentRate">
        <template slot-scope="scope">
          <span>{{ calculatePercentage(scope.row.already, scope.row.detailsCount) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="220px;">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:insurance:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-hasPermi="['cld:insurance:claims']" size="mini" type="text"
                     @click="handleViewClaims(scope.row)">查看理赔
          </el-button>
          <el-button v-hasPermi="['cld:insurance:related']" size="mini" type="text"
                     @click="handleRelatedPersonnel(scope.row)">关联人员
          </el-button>
          <el-button v-hasPermi="['cld:insurance:remove']" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改保险管理对话框 -->
    <el-dialog :visible.sync="open" append-to-body title="保险管理" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="保险名称" prop="insuranceName">
              <el-input v-model="form.insuranceName" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保险单号" prop="insuranceOdd">
              <el-input v-model="form.insuranceOdd" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="保险类型" prop="insuranceType">
              <el-select v-model="form.insuranceType" placeholder="请选择">
                <el-option v-for="dict in dict.type.cld_insurance_type" :key="dict.value" :label="dict.label"
                           :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保险额度" prop="insuranceQuato">
              <el-input v-model="form.insuranceQuato" placeholder="请输入" type="number">
                <template slot="append">万</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="保险时间" prop="timeInterval">
              <el-date-picker v-model="form.timeInterval" end-placeholder="结束日期" range-separator="至"
                              start-placeholder="开始日期"
                              type="daterange">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="60岁以上" prop="maxPrice">
              <el-input v-model="form.maxPrice" placeholder="含60岁" type="number">
                <template slot="append">元/人</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="60岁以下" prop="minPrice">
              <el-input v-model="form.minPrice" placeholder="请输入" type="number">
                <template slot="append">元/人</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="保险责任" prop="responsibility">
              <el-input v-model="form.responsibility" :rows="4" placeholder="请输入" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="赔付特约" prop="agreement">
              <el-input v-model="form.agreement" :rows="4" placeholder="请输入" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addInsurance, delInsurance, getInsurance, listInsurance, updateInsurance} from "@/api/cld/insurance";

import analyze from './analyze.vue';

export default {
  name: "Insurance",
  components: {
  },
  dicts: ['cld_insurance_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保险管理表格数据
      insuranceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insuranceOdd: null,
        insuranceName: null,
        insuranceStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        insuranceName: [
          {required: true, message: "请输入保险名称", trigger: "blur"},
        ],
        insuranceType: [
          {required: true, message: "请选择保险类型", trigger: "blur"},
        ],
        insuranceOdd: [
          {required: true, message: "请输入保险单号", trigger: "blur"},
        ],
        insuranceQuato: [
          {required: true, message: "请输入保险额度", trigger: "blur"},
        ],
        timeInterval: [
          {required: true, message: "请选择保险时间", trigger: "blur"},
        ],
        maxPrice: [
          {required: true, message: "请输入60岁以上保价", trigger: "blur"},
        ],
        minPrice: [
          {required: true, message: "请输入60岁以下保价", trigger: "blur"},
        ],
        responsibility: [
          {required: true, message: "请输入保险责任", trigger: "blur"},
        ],
        agreement: [
          {required: true, message: "请输入赔付特约", trigger: "blur"},
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询保险管理列表 */
    getList() {
      this.loading = true;
      listInsurance(this.queryParams).then(response => {
        this.insuranceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        insuranceId: null,
        insuranceName: null,
        insuranceQuato: null,
        startTime: null,
        endTime: null,
        insuranceStatus: null,
        responsibility: null,
        agreement: null,
        timeInterval: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保险管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const insuranceId = row.insuranceId || this.ids
      getInsurance(insuranceId).then(response => {
        this.form = response.data;
        this.form.timeInterval = [this.parseTime(this.form.startTime), this.form.endTime];
        this.open = true;
        this.title = "修改保险管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.startTime = this.form.timeInterval[0];
          this.form.endTime = this.form.timeInterval[1];
          if (this.form.insuranceId != null) {
            updateInsurance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInsurance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const insuranceIds = row.insuranceId || this.ids;
      this.$modal.confirm('是否确认删除保险管理编号为"' + insuranceIds + '"的数据项？').then(function () {
        return delInsurance(insuranceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 关联人员
    handleRelatedPersonnel(row) {
      this.$router.push("/insurance/related/personnel/" + row.insuranceId);
    },
    // 查看理赔
    handleViewClaims(row) {
      this.$emit('claims', row)
    },
    handleExport(type,fileName) {
      this.download('cld/insurance/export', {type: type}, `${fileName}.xlsx`)
    }
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}
</style>
